<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<script type="text/javascript">
			function cloneDeep(obj) {
				// debugger
				let objClone = Array.isArray(obj) ? [] : {};
				if (obj && typeof obj === "object") {
					for (key in obj) {
						if (obj.hasOwnProperty(key)) {
							// 判断ojb子元素是否为对象，如果是，递归复制
							if (obj[key] && typeof obj[key] === "object") {
								objClone[key] = cloneDeep(obj[key]);
							} else {
								// 如果不是，简单复制
								objClone[key] = obj[key];
							}
							// console.log(objClone, "start")
						}
					}
				}
				// console.log(objClone, "end")
				return objClone;
			}
			function cloneDeepOC(initalObj, finalObj) {
				var obj = finalObj || {};
				for (var i in initalObj) {
					var prop = initalObj[i]; // 避免相互引用对象导致死循环，如initalObj.a = initalObj
					if (prop === obj) {
						continue;
					}
					if (typeof prop === 'object') {
						obj[i] = (prop.constructor === Array) ? [] : Object.create(prop);
					} else {
						obj[i] = prop;
					}
				}
				return obj;
			}
			let obj = {
				a: "a",
				b: "b",
				c: {
					c2: "c2"
				},
				d: {
					d2: {
						d3: "d3"
					}
				}
			}
			// const obj2 = JSON.parse(JSON.stringify(obj))
			console.time('运算时间')
			const obj2 = cloneDeepOC(obj)
			console.timeEnd('运算时间')
			obj2.a = 0
			obj2.c = {
				c2: 1,
				ca: 2
			}
			console.log(obj, obj2)
		</script>
	</body>
</html>